from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.views.decorators.csrf import csrf_exempt
from django.db.models import Q
from django.core.paginator import Paginator
from django.contrib import messages

# @csrf_exempt  # 忽略 csrf的校验
from blog.forms import CategoryForm, ArticleForm
from blog.models import Category, Article


# def add(request):
#     if request.method == 'GET':
#         return render(request, 'category/add.html')
#     elif request.method == 'POST':
#         # CSRF验证失败
#         name = request.POST.get('name')
#         comment = request.POST.get('comment')
#         Category.objects.create(name=name, comment=comment)
#         return HttpResponse('类别插入成功')


# def add(request):
#     if request.method == 'GET':
#         form = CategoryForm()
#         return render(request, 'category/add.html', {'form': form})
#     elif request.method == 'POST':
#         # CSRF验证失败
#         # name = request.POST.get('name')
#         # comment = request.POST.get('comment')
#
#         form = CategoryForm(request.POST)
#         if form.is_valid():
#             name = form.cleaned_data['name']
#             comment = form.cleaned_data['comment']
#             Category.objects.create(name=name, comment=comment)
#             return HttpResponse('类别插入成功')
#         else:
#             return render(request, 'category/add.html', {'form': form})


# 增删改查

def clist(request):
    # ?page=1&q=django
    # 我们的关键字能够实现对标题以及内容的模糊匹配
    q = request.GET.get('q', '')
    page = request.GET.get('page')

    article_list = Article.objects.filter(
        Q(title__contains=q) | Q(content__contains=q))

    # from blogproject import settings
    # from django.conf import settings  优秀，因为不依赖于blogproject的项目名
    paginator = Paginator(article_list, settings.PER_PAGE)  # 每页展示两条记录
    articles = paginator.get_page(page)  # 获取第page页的数据

    return render(request, 'article/list.html', {'articles': articles})


def add(request):
    if request.method == 'GET':
        # form = ArticleForm(initial={'title': '默认标题'})
        form = ArticleForm()
    elif request.method == 'POST':
        form = ArticleForm(request.POST)
        if form.is_valid():
            form.save()
            messages.add_message(request, messages.SUCCESS, '添加成功！')
            return redirect('article:list')
    return render(
        request, 'article/add.html', {'form': form, 'title': '添加文章'})


def edit(request, pk):
    article = get_object_or_404(Article, pk=pk)
    if request.method == 'GET':
        form = ArticleForm(instance=article)
    elif request.method == 'POST':
        form = ArticleForm(request.POST, instance=article)
        if form.is_valid():
            form.save()
            messages.add_message(request, messages.SUCCESS, '修改成功！')
            return redirect('article:list')
    return render(
        request, 'article/add.html', {'form': form, 'title': '编辑文章'})


def delete(request, pk):
    article = get_object_or_404(Article, pk=pk)
    article.delete()
    messages.add_message(request, messages.SUCCESS, '删除成功！')
    return redirect('article:list')

# flask 消息闪现
# django messages
